<?php
namespace Admin\Controller;
use Admin\Controller\BaseController;
use Common\ORG\Util\Page;
use Common\ORG\Util\UploadFile;

/**
 * 客户
 * @author Evan <tangzwgo@foxmail.com>
 * @since 2016年7月23日
 */
class CustomerController extends BaseController {
	/**
	 * 构造函数
	 */
	public function __construct() {
		//无需进行权限检测的功能
		\Org\Auth\AuthUtil::setNoAuthFuncList([
		    '/admin/customer/editOrder',
		    '/admin/customer/customerDetail',
            '/admin/customer/yzAuditingEditCustomer',
            '/admin/customer/customerFollow',
			'/admin/customer/ajaxGetCate',
			'/admin/customer/ajaxDirectOrder',
			'/admin/customer/ajaxUploadQualification',
			'/admin/customer/ajaxChangeCustomerOrder',
        ]);
		parent::__construct();
	}
	
	/**
	 * 客户列表(业务员)
	 */
	public function customerList() {
        $type = trim(I('get.type'));
        $company_name = trim(I('get.company_name'));
        $phase = intval(I('get.phase'));
        $page = intval(I('get.page',1));
        $search_time = I("get.search_time");
        $start = '';
        $end = '';
        if($search_time){
            $all_time = explode('-',$search_time);
            $start = strtotime($all_time[0]);
            $end = strtotime($all_time[1]." 23:59:59");
        }
        $page_size = 15;
        //用户信息
        $user = self::$user;

        $where = array();
        isset($page) && $where['page'] = $page;
        isset($page_size) && $where['page_size'] = $page_size;
        $company_name && $where['company_name'] = $company_name;
        $where['status'] = ['in',[0,1]];
        if($type && $type != -1){
            $where['type'] = $type;
        }
        if($phase && $phase != -1){
            $where['phase'] = $phase;
        }
        $start && $where['start'] = $start;
        $end && $where['end'] = $end;
        $where['admin_id'] = $user['id'];

        $result = D('Customer','Logic')->searchCustomerList($where);

        //客户类别
        $typeList = C('CUSTOMER_TYPE');
        $phaseList = C('CUSTOMER_PHASE');

        $page = $result['page'];
        $page_size = $result['page_size'];
        $total = $result['total'];
        $total_page = $result['total_page'];
        $customerList = $result['dataList'];

        //生成分页
        $url = "/customer/customerList?search_time={$search_time}&company_name={$company_name}&phase={$phase}&type={$type}";
        $page_html = create_page($url, $total, $page, $page_size);

        $this->assign('page', $page);
        $this->assign('page_size', $page_size);
        $this->assign('total', $total);
        $this->assign('total_page', $total_page);
        $this->assign('page_html', $page_html);

        $this->assign('type', $type);
        $this->assign('search_time', $search_time);
        $this->assign('company_name', $company_name);
        $this->assign('phase', $phase);
        $this->assign('typeList', $typeList);
        $this->assign('phaseList', $phaseList);
		$this->assign('customerList', $customerList);

		$this->loadFrame('customerList');
	}

    /**
     * 客户详情
     */
	public function customerDetail(){
        $customer_id = I("get.id");
        if(!$customer_id){
            header('Location:/customer/customerList');
            exit();
        }
        //客户信息
        $customerInfo = D('Customer','Logic')->getCustomerInfo(['id'=>$customer_id]);
        $phaseList = C('CUSTOMER_PHASE');

        $this->assign('customer',$customerInfo);
        $this->assign('phaseList',$phaseList);
        $this->loadFrame('customerDetail');
    }


    /**
     * 被拒客户列表(业务员)
     */
    public function refuseCustomerList() {
        $page = intval(I('get.page',1));
        $page_size = 15;
        //用户信息
        $user = self::$user;

        $where = array();
        $where['page'] = $page;
        $where['page_size'] = $page_size;
        $where['status'] = 5;
        $where['admin_id'] = $user['id'];

        $result = D('Customer','Logic')->searchCustomerList($where);

        //客户类别
        $typeList = C('CUSTOMER_TYPE');
        $phaseList = C('CUSTOMER_PHASE');

        $page = $result['page'];
        $page_size = $result['page_size'];
        $total = $result['total'];
        $total_page = $result['total_page'];
        $customerList = $result['dataList'];

        //生成分页
        $url = "/customer/refuseCustomerList";
        $page_html = create_page($url, $total, $page, $page_size);

        $this->assign('page', $page);
        $this->assign('page_size', $page_size);
        $this->assign('total', $total);
        $this->assign('total_page', $total_page);
        $this->assign('page_html', $page_html);

        $this->assign('typeList', $typeList);
        $this->assign('phaseList', $phaseList);
        $this->assign('customerList', $customerList);

        $this->loadFrame('refuseCustomerList');
    }

	/**
     * 编辑客户信息
     */
	public function editCustomer(){
        $id = I('get.id');
        $customer = [];
        if($id){
            $customer = D('Customer','Logic')->getCustomerInfo(['id'=>$id]);
        }

        //客户类别
        $typeList = C('CUSTOMER_TYPE');
        $levelList = C('CUSTOMER_LEVEL');

        $p_cate = D('Resource/ItemCate','Service')->getAllCateList(['status'=>1,'depth'=>1]);
		$two_cate = array();
		if($customer){
			$two_cate = D('Resource/ItemCate','Service')->getAllCateList(['pid'=>$customer['cid'],'status'=>1]);
		}

        $this->assign('typeList',$typeList);
        $this->assign('levelList',$levelList);
        $this->assign('customer',$customer);
        $this->assign('p_cate',$p_cate);
        $this->assign('two_cate',$two_cate);
        $this->loadFrame('editCustomer');
    }

    /**
     * @return string
     */
    public function ajaxSaveCustomer(){
        $user = self::$user;
        $id = I('post.id');
        $admin_id = I('post.admin_id');
        $cid = I('post.cid');
        $name = I('post.name');
        $company_name = I('post.company_name');
        $product_name = I('post.product_name');
        $level = I('post.level');
        $type = I('post.type');
        $gender = I('post.gender');
        $mobile = I('post.mobile');
        $wechat = I('post.wechat');
        $qq = I('post.qq');
        $tel = I('post.tel');
        $second_cid = I('post.second_cid');

        $data = [];
        $id && $data['id'] = $id;
        $cid && $data['cid'] = $cid;
        $admin_id && $data['admin_id'] = $admin_id;
        $name && $data['name'] = $name;
        $company_name && $data['company_name'] = $company_name;
        $product_name && $data['product_name'] = $product_name;
        $level && $data['level'] = $level;
        $type && $data['type'] = $type;
        $gender && $data['gender'] = $gender;
        $mobile && $data['mobile'] = $mobile;
        $wechat && $data['wechat'] = $wechat;
        $qq && $data['qq'] = $qq;
        $tel && $data['tel'] = $tel;
        $data['second_cid'] = $second_cid;
        if(!$id){
           $data['admin_id'] = $user['id'];
        }

        if($admin_id && ($admin_id!=$user['id'])){
            return Response(201,"无权操作");
        }

        if(!$data){
            return Response(202,"无需操作");
        }
        if(!$id){
            $count = D('Customer','Logic')->getAdminCustomerCount(['admin_id'=>$data['admin_id']]);
            //用户组
            $userGroup = D('Resource/User','Service')->getUserRelationGroupInfo(['au.id'=>$data['admin_id']]);
            if($count>=$userGroup['total_enrollment']){
                return Response(203,"录入客户已达上限");
            }
        }
        D('Customer','Logic')->saveCustomer($data);

    }

    /**
     * 跟进客户
     */
    public function customerFollow(){
        $customer_id = intval(I('get.id'));
        $page = intval(I('get.page',1));
        $page_size = 10;
        if(!$customer_id){
            header('Location:/admin/customer/customerList');
            exit();
        }
        $data = array();
        $data['page'] = $page;
        $data['page_size'] = $page_size;
        $data['customer_id'] = $customer_id;

        $result = D('PhaseLog','Logic')->searchPhaseLogList($data);
        $page = $result['page'];
        $page_size = $result['page_size'];
        $total = $result['total'];
        $total_page = $result['total_page'];
        $logList = $result['dataList'];

        //生成分页
        $url = "/customer/customerFollow?id={$customer_id}";
        $page_html = create_page($url, $total, $page, $page_size);

        $this->assign('page', $page);
        $this->assign('page_size', $page_size);
        $this->assign('total', $total);
        $this->assign('total_page', $total_page);
        $this->assign('page_html', $page_html);

        $this->assign('logList',$logList);
        $this->assign('customer_id',$customer_id);
        $this->loadFrame('customerFollow');
    }

    /**
     * 跟进客户保存
     */
    public function ajaxSavePhaseLog(){
        $user = self::$user;
        $customer_id = intval(I('post.customer_id'));
        $description = trim(I('post.description'));
        if(!$customer_id){
            return Response(2001,"参数异常");
        }
        if(!$description){
            return Response(2002,"请填写跟进信息");
        }

        $data = [];
        $data['customer_id'] = $customer_id;
        $data['description'] = $description;
        $data['admin_id'] = $user['id'];

        D('PhaseLog','Logic')->ajaxSavePhaseLog($data);
    }

    /**
     * 客户阶段更新
     */
    public function updateCustomerPhase(){
        $customer_id = intval(I('post.customer_id'));
        $phase = intval(I('post.phase'));

        if(!$customer_id){
            return Response('201','参数错误！');
        }else{
            $data['id'] = $customer_id;
        }
        $customerInfo = D('Resource/Customer','Service')->getCustomerInfo(['id'=>$customer_id]);
        if(!$customerInfo){
            return Response(2003,"未查询到该客户信息");
        }
        if($customerInfo['status']!=1){
            return Response(2005,"该客户暂不可进行操作");
        }
        if(!$phase){
            return Response('202','请选择阶段！');
        }else{
            $data['phase'] = $phase;
        }
        $res = D('Resource/Customer','Service')->updateCustomerInfo($data);
        if($res){
            return Response('999','更新成功');
        }else{
            return Response('203','更新失败');
        }
    }

    /**
     * 拒绝客户
     */
    public function customerRefuse(){
        $customer_id = intval(I('post.customer_id'));
        $refuse_reason = trim(I('post.refuse_reason'));

        if(!$customer_id){
            return Response('2001','参数错误！');
        }else{
            $data['id'] = $customer_id;
        }
        if(!$refuse_reason){
            return Response('2001','请填写终止服务备注');
        }else{
            $data['refuse_reason'] = $refuse_reason;
        }
        $res = D('Resource/Customer','Service')->updateCustomerInfo($data);
        if($res){
            return Response('999','更新成功');
        }else{
            return Response('2001','更新失败');
        }

    }

    /**
     * 需求明确
     */
    public function customerDemandClear(){
        $customer_id = intval(I('get.id'));
        $customer = D('Resource/Customer')->getCustomerInfo(['id'=>$customer_id]);

        $data1 = [];
        $data1['status'] = 1;
        $data1['depth'] = 1;
        $p_cate = D('Resource/ItemCate','Service')->getAllCateList($data1);

        $data = [];
        $data['pid'] = $customer['cid'];
        $data['status'] = 1;
        $data['depth'] = 2;
        //二级分类
        $second_cate = D('Resource/ItemCate','Service')->getAllCateList($data);
        //客户类别
        $regimentation = D('Resource/Regimentation')->getCategoryList([]);

        $this->assign('regimentation',$regimentation);
        $this->assign('customer',$customer);
        $this->assign('p_cate',$p_cate);
        $this->assign('second_cate',$second_cate);
        $this->loadFrame('customerDemandClear');

    }

    /**
     * @return string
     */
    public function ajaxGetCate(){
        $pid = I('post.pid');
        $list = D('Resource/ItemCate','Service')->getAllCateList(['pid'=>$pid,'status'=>1]);
        $html = '';
        if($list){
			$html = '<option value="">请选择</option>';
            foreach ($list as $v){
                $html .= '<option value="'.$v['id'].'">'.$v['name'].'</option>';
            }
        }
        echo $html;
        exit();
    }
    /**
     * 业务员发票列表
     */
    public function salesmanBillList(){
        //分页
        $page = I('get.page', 1);
        $pagesize = 15;
        $company_name = I('get.company_name');
        $type = I('get.type',0);
        $flag = I('get.flag',0);
        //用户信息
        $user = self::$user;

        $params = array();
        $params['page'] = $page;
        $params['page_size'] = $pagesize;
        $params['admin_id'] = $user['id'];
        $company_name && $params['company_name'] = $company_name;
        $type && $params['type'] = $type;

        switch ($flag){
            case 1:
                $params['status'] = ['lt',15];
                break;
            case 2:
                $params['status'] = 15;
                break;
            default:
                break;
        }

        $result = D('OrderBill','Logic')->searchOrderBillList($params);

        $page = $result['page'];
        $page_size = $result['page_size'];
        $total = $result['total'];
        $total_page = $result['total_page'];
        $orderList = $result['dataList'];
        //生成分页
        $url = "/customer/salesmanBillList?flag={$flag}&company_name={$company_name}&type={$type}";
        $page_html = create_page($url, $total, $page, $page_size);

        $bill_typeList = C('BILL_TYPE');

        $this->assign('bill_typeList',$bill_typeList);
        $this->assign('page_html',$page_html);
        $this->assign('page',$page);
        $this->assign('page_size',$page_size);
        $this->assign('total_page',$total_page);
        $this->assign('total',$total);
        $this->assign('billList',$orderList);

        $this->assign('company_name', $company_name);
        $this->assign('type', $type);
        $this->assign('flag', $flag);
        $this->loadFrame('salesmanBillList');
    }
    /**
     * 发票详情
     */
    public function orderBillDetail(){
        $user = self::$user;
        $id = I("get.id");
        if(!$id){
            header('Location:/customer/salesmanBillList');
            exit();
        }
        $data = [];
        $data['id'] = $id;
        $data['admin_id'] = $user['id'];
        $billInfo = D('OrderBill','Logic')->getOrderBillInfo($data);

        $bill_typeList = C('BILL_TYPE');

        $this->assign('bill_typeList',$bill_typeList);
        $this->assign('billInfo',$billInfo);
        $this->loadFrame('orderBillDetail');
    }

    /**
     * @return string
     */
    public function ajaxDeleteOrderBill(){
        $user = self::$user;
        $bill_id = I('post.bill_id');
        if(!$bill_id){
           return Response(201,'请求参数异常');
        }
        $data = [];
        $data['bill_id'] = $bill_id;
        $data['admin_id'] = $user['id'];
        D('OrderBill','Logic')->deleteOrderBill($data);
    }

    /**
     * 下单（业务员）
     */
    public function ajaxGetOrderInfo(){
        $user = self::$user;
        $customer_id = I('post.customer_id');
        $order_id = I('post.order_id');
        $data = [];
        $data['customer_id'] = $customer_id;
        $order_id && $data['order_id'] = $order_id;
        $data['admin_id'] = $user['id'];

        D('Order','Logic')->addOrderByCustomer($data);
    }
    /**
     * 业务员订单详情
     */
    public function editOrder(){
        $order_id = I("get.order_id");
        if(!$order_id){
            header('Location:/customer/customerList');
            exit();
        }
        $data = [];
        $data['id'] = $order_id;
        $orderInfo = D('Order','Logic')->getOrderDetail($data);

        $itemList = $orderInfo['itemList'];
        //客户信息
        $customerInfo = D('Customer','Logic')->getCustomerInfo(['id'=>$orderInfo['customer_id']]);
        //二级分类信息
        $sCate = D('Resource/ItemCate','Service')->getCateInfo(['id'=>$customerInfo['second_cid']]);
        //三级分类
        $three_cate = D('Resource/ItemCate','Service')->getAllCateList(['status'=>1,'depth'=>3,'pid'=>$customerInfo['second_cid']]);
        //类别
        $typeList = C('CUSTOMER_TYPE');

        $this->assign('order_id',$order_id);
        $this->assign('orderInfo',$orderInfo);
        $this->assign('itemList',$itemList);
        $this->assign('typeList',$typeList);
        $this->assign('customer',$customerInfo);
        $this->assign('sCate',$sCate);
        $this->assign('three_cate',$three_cate);
        $this->loadFrame('editOrder');
    }
    /**
     * 申请报价（业务员）
     * @return string
     */
    public function ajaxApplyQuoted(){
        $user = self::$user;
        $order_id = I('post.order_id');

        if(!$order_id){
            return Response(201,"参数异常");
        }
        $data = [];
        $data['order_id'] = $order_id;
        $data['uid'] = $user['id'];

        D('Order','Logic')->applyQuoted($data);
    }
    /**
     * 申请排期（业务员）
     * @return string
     */
    public function ajaxApplySchedule(){
        $user = self::$user;
        $order_id = I('post.order_id');

        if(!$order_id){
            return Response(201,"参数异常");
        }
        $data = [];
        $data['order_id'] = $order_id;
        $data['uid'] = $user['id'];

        D('Order','Logic')->applySchedule($data);
    }

    /**
     * 终止服务（订单完成）
     * @return string
     */
    public function ajaxServiceCompletion(){
        $user = self::$user;
        $order_id = I('post.order_id');

        if(!$order_id){
            return Response(201,"参数异常");
        }
        $data = [];
        $data['order_id'] = $order_id;
        $data['uid'] = $user['id'];

        D('Order','Logic')->orderServiceCompletion($data);
    }

    /**
     * 直接下单
     * @return string
     */
    public function ajaxDirectOrder(){
        $user = self::$user;
        $order_id = I('post.order_id');

        if(!$order_id){
            return Response(201,"参数异常");
        }
        $data = [];
        $data['order_id'] = $order_id;
        $data['uid'] = $user['id'];

        D('Order','Logic')->orderDirectOrder($data);
    }
    /**
     * 业务员下单保存
     */
    public function ajaxToSaveOrder(){
        $param = I('post.');
        $user = self::$user;
        $order_id = isset($param['order_id']) ? intval($param['order_id']) : '';
        $name = isset($param['name']) ? strval($param['name']) : '';
        $agent_name = isset($param['agent_name']) ? strval($param['agent_name']) : '';
        $company_name = isset($param['company_name']) ? strval($param['company_name']) : '';
        $ditch_company = isset($param['ditch_company']) ? strval($param['ditch_company']) : '';
        $mobile = isset($param['mobile']) ? strval($param['mobile']) : '';
        $product_name = isset($param['product_name']) ? strval($param['product_name']) : '';
        $amount= isset($param['amount']) ? floatval($param['amount']) : '';
        $quoted_price = isset($param['quoted_price']) ? floatval($param['quoted_price']) : '';
        $recharge_price = isset($param['recharge_price']) ? floatval($param['recharge_price']) : '';
        $pay_interval = isset($param['pay_interval']) ? intval($param['pay_interval']) : '';
        $rebate = isset($param['rebate']) ? intval($param['rebate']) : 0;
        $remark = isset($param['remark']) ? strval($param['remark']) : '';
        $is_filing = isset($param['is_filing']) ? intval($param['is_filing']) : 0;
        $is_bill = isset($param['is_bill']) ? intval($param['is_bill']) : 0;
        $is_pay = isset($param['is_pay']) ? intval($param['is_pay']) : 0;
        if(!$order_id)
            return Response(2001,'数据异常');
        if(!$name)
            return Response(2001,'请填写联系人');
        if(!$company_name)
            return Response(2001,'请填写终端客户名称');
        if(!$agent_name)
            return Response(2001,'请填写公司名（代理商）');
        if(!preg_match("/^1[345678]\d{9}$/",$mobile))
            return Response(2001,'请正确填写手机号');
        if(!$product_name)
            return Response(2001,'请填写项目名');
        /*
        if($is_pay == 1 && !$quoted_price)
            return Response(2001,'请填写支付金额');
        if($is_pay == 1 && !$amount)
            return Response(2002,'请填销售金额');
        */
       $rate = 0;
        if($rebate>0){
            $rate = round($rebate/100,2);
        }
        $other_price = round($amount*$rate);
        $profit_price = $amount-$quoted_price-round($amount*$rate);
        if($profit_price<0){
            $profit_price =0;
        }
        if(!$is_pay){
           $pay_amount = 0;
        }else{
            $pay_amount = $quoted_price;
        }
        $data = array();
        $data['id'] = $order_id;
        $data['uid'] = $user['id'];
        $data['name'] = $name;
        $data['company_name'] = $company_name;
        $data['amount'] = $amount;
        $data['remark'] = $remark;
        $data['is_filing'] = $is_filing;
        $data['is_bill'] = $is_bill;
        $data['is_pay'] = $is_pay;
        $data['pay_amount'] = $pay_amount;
        $data['recharge_price'] = $recharge_price;
        $data['other_price'] = $other_price;
        $data['profit_price'] = $profit_price;
        $data['pay_interval'] = $pay_interval;
        $data['rebate'] = $rebate;
        $data['status'] = 30;
        $data['agent_name'] = $agent_name;
        $data['order_time'] = time();
        $data['ditch_company'] = $ditch_company;
        $data['product_name'] = $product_name;
        $data['mobile'] = $mobile;
        D('Order','Logic')->ajaxToSaveOrder($data);
    }

    /**
     * 业支客户审核列表
     */
    public function yzAuditingCustomerList() {
        $type = trim(I('get.type'));
        $company_name = trim(I('get.company_name'));
        $admin_name = trim(I('get.admin_name'));
        $phase = intval(I('get.phase'));
        $page = intval(I('get.page',1));
        $flag = I('get.flag',0);
        $search_time = I("get.search_time");
        $start = '';
        $end = '';
        if($search_time){
            $all_time = explode('-',$search_time);
            $start = strtotime($all_time[0]);
            $end = strtotime($all_time[1]." 23:59:59");
        }
        $page_size = 15;
        //用户信息
        $user = self::$user;

        $where = array();
        isset($page) && $where['page'] = $page;
        isset($page_size) && $where['page_size'] = $page_size;
        $company_name && $where['company_name'] = $company_name;
        if($type && $type != -1){
            $where['type'] = $type;
        }
        if($phase && $phase != -1){
            $where['phase'] = $phase;
        }
        if($user['department_id'] == 5){
            $where['admin_id'] = $user['id'];
        }
        switch ($flag){
            case 1:
                $where['status'] = ['lt',1];
                break;
            case 2:
                $where['status'] = 1;
                break;
            default:
                break;
        }
        $start && $where['start'] = $start;
        $end && $where['end'] = $end;
        $admin_name && $where['admin_name'] = $admin_name;
        $result = D('Customer','Logic')->searchCustomerList($where);

        //客户类别
        $typeList = C('CUSTOMER_TYPE');
        $phaseList = C('CUSTOMER_PHASE');

        $page = $result['page'];
        $page_size = $result['page_size'];
        $total = $result['total'];
        $total_page = $result['total_page'];
        $customerList = $result['dataList'];

        //生成分页
        $url = "/customer/yzAuditingCustomerList?admin_name={$admin_name}&search_time={$search_time}&flag={$flag}&company_name={$company_name}&phase={$phase}&type={$type}";
        $page_html = create_page($url, $total, $page, $page_size);

        $departmentList = D('Resource/User',"Service")->getDepartmentList([]);

        $this->assign('page', $page);
        $this->assign('page_size', $page_size);
        $this->assign('total', $total);
        $this->assign('total_page', $total_page);
        $this->assign('page_html', $page_html);

        $this->assign('type', $type);
        $this->assign('search_time', $search_time);
        $this->assign('flag', $flag);
        $this->assign('company_name', $company_name);
        $this->assign('admin_name', $admin_name);
        $this->assign('phase', $phase);
        $this->assign('typeList', $typeList);
        $this->assign('phaseList', $phaseList);
        $this->assign('customerList', $customerList);
        $this->assign('departmentList', $departmentList);

        $this->loadFrame('yzAuditingCustomerList');
    }

    /**
     * 业支客户信息
     */
    public function yzAuditingEditCustomer(){
        $id = I('get.id');

        if(!$id){
            header('Location:/admin/customer/yzCustomerList');
            exit();
        }
        $customer = D('Customer','Logic')->getCustomerInfo(['id'=>$id]);

        //客户类别
        $typeList = C('CUSTOMER_TYPE');
        $levelList = C('CUSTOMER_LEVEL');
        $statusList = C('CUSTOMER_STATUS');
        $phaseList = C('CUSTOMER_PHASE');

        $data = [];
        $data['status'] = 1;
        $data['depth'] = 1;
        $p_cate = D('Resource/ItemCate','Service')->getAllCateList($data);

        $this->assign('typeList',$typeList);
        $this->assign('levelList',$levelList);
        $this->assign('statusList',$statusList);
        $this->assign('phaseList',$phaseList);
        $this->assign('customer',$customer);
        $this->assign('p_cate',$p_cate);
        $this->loadFrame('yzAuditingEditCustomer');
    }

    /**
     * 业支审核客户
     * @return string
     */
    public function ajaxAuditingCustomer(){
        $user = self::$user;
        $id = I('post.id');
        $status = I('post.status');
        $refuse_reason = I('post.refuse_reason');
        $type = I('post.type');

        if(!$id){
            return Response(201,"参数异常");
        }
        if($user['department_id']!=1){
            return Response(202,"无审核权限");
        }
        $data = [];
        $data['id'] = $id;
        $data['status'] = $status;
        $data['type'] = $type;
        $refuse_reason && $data['refuse_reason'] = $refuse_reason;

        D('Customer','Logic')->yzAuditingCustomer($data);
    }

    /**
     * 业务员客户申领
     * @return string
     */
    public function ajaxAutoApplicantCustomer(){
        $user = self::$user;
        $company_name = I('post.company_name');
        $product_name = I('post.product_name');
        $cid = I('post.cid');

        if(!$company_name || !$product_name){
            return Response(201,"网络请求异常");
        }
        $data = [];
        $data['company_name'] = $company_name;
        $data['product_name'] = $product_name;
        $data['admin_id'] = $user['id'];
        $data['cid'] = $cid;

        D('Customer','Logic')->autoApplicantCustomer($data);
    }
    /**
     * 公共客户列表
     */
    public function commonCustomerList() {
        $type = trim(I('get.type'));
        $company_name = trim(I('get.company_name'));
        $phase = intval(I('get.phase'));
        $department_id = intval(I('get.department_id'));
        $page = intval(I('get.page',1));
        $page_size = 15;
        //用户信息
        $user = self::$user;

        $data = array();
        isset($page) && $data['page'] = $page;
        isset($page_size) && $data['page_size'] = $page_size;
        $company_name && $data['company_name'] = $company_name;
        $data['status'] = 10;
        if($department_id && $department_id != -1){
            $data['department_id'] = $department_id;
        }
        if($type && $type != -1){
            $data['type'] = $type;
        }
        if($phase && $phase != -1){
            $data['phase'] = $phase;
        }

        $result = D('Customer','Logic')->searchCommonCustomerList($data);

        //客户类别
        $typeList = C('CUSTOMER_TYPE');
        $phaseList = C('CUSTOMER_PHASE');

        $page = $result['page'];
        $page_size = $result['page_size'];
        $total = $result['total'];
        $total_page = $result['total_page'];
        $customerList = $result['dataList'];

        //生成分页
        $url = "/admin/customer/commonCustomerList?department_id={$department_id}&company_name={$company_name}&phase={$phase}&type={$type}";
        $page_html = create_page($url, $total, $page, $page_size);

        $departmentList = D('Resource/User',"Service")->getDepartmentList([]);

        $this->assign('page', $page);
        $this->assign('page_size', $page_size);
        $this->assign('total', $total);
        $this->assign('total_page', $total_page);
        $this->assign('page_html', $page_html);

        $this->assign('type', $type);
        $this->assign('company_name', $company_name);
        $this->assign('phase', $phase);
        $this->assign('department_id', $department_id);
        $this->assign('typeList', $typeList);
        $this->assign('departmentList', $departmentList);
        $this->assign('phaseList', $phaseList);
        $this->assign('customerList', $customerList);

        $this->loadFrame('commonCustomerList');
    }
    /**
     * 资质上传
     * @return string
     */
    public function ajaxUploadQualification(){
        set_time_limit(0);
        ini_set('memory_limit', -1);
        $customer_id = I('post.customer_id');
        if(!$customer_id){
            return Response(201,"参数异常");
        }
        if (!empty($_FILES['import']['name'])) {
            $upload = new UploadFile();
            $upload->maxSize = 93145728;
            $upload->saveRule = "qualification_".$customer_id."_detail_".time();
            $upload->allowExts = array('png','jpg','jpeg','zip','rar','7z');
            $upload->savePath = C('STATIC_PATH') . '/attachment/file/customer/';
            if (!$upload->upload()) {
                return Response(2001,$upload->getErrorMsg());
            }
            //导入数据
            $info = $upload->getUploadFileInfo();

            $data = [];
            $data['id']  = $customer_id;
            $data['upfile'] = '/attachment/file/customer/'.$info[0]['savename'];
            $res = D('Resource/Customer','Service')->updateCustomerInfo($data);
            if(!$res){
                return Response(2003,'网络请求失败');
            }
            return Response(999,'请求成功');
        }else{
            return Response(2002,"请选择要上传的文件");
        }
    }

    /**
     * 转单
     */
    public function ajaxChangeCustomerOrder(){
        $department_id = I('post.department_id');
        $admin_id = I('post.admin_id');
        $chooseList = I('post.chooseList');

        if(!$department_id || !$admin_id || !$chooseList){
            return Response(201,"参数异常");
        }
        $data = [];
        $data['department_id'] = $department_id;
        $data['admin_id'] = $admin_id;
        $data['chooseList'] = $chooseList;

        D('Customer','Logic')->ajaxChangeCustomerOrder($data);
    }
}